#include "allincludes.h"

int FileReader::SwitchToFile(string newFileFullName)
{
	if(!FileTest(newFileFullName))
	{
		cout<<"Error : Cannot switch to file: "<<newFileFullName<<endl;
		return FAILED;
	}
	if(file.is_open())
	{
		file.close();
	}
	file.open(newFileFullName.c_str(),ios::in|ios::binary);
	file.seekg(0, ios::beg);
	
	return SUCCEED;
}
int FileReader::FileTest(string newFileFullName)
{
	fstream tempFile;
	tempFile.open(newFileFullName.c_str(), ios::in|ios::binary);
	if(!tempFile)
	{
		cout<<"FileTest: Cannot Open File: "<<newFileFullName<<endl;
		return FAILED;
	}
	tempFile.seekg(0, ios::end);
	int tempFileSize=tempFile.tellg();
	tempFile.seekg(0, ios::beg);
	if(tempFileSize<120)
	{
		cout<<"The file is too small, might have been damaged"<<endl;
		tempFile.close();
		return FAILED;
	}
	OneLine tester;
//	tempFile.read((char*)&tester.headSize, sizeof(int));
//	if(tester.headSize!=HEADSIZE)
//	{
//		cout<<"HEADSIZE in the program is "<<HEADSIZE<<", but in the file is "<<tester.headSize<<endl;
//		tempFile.close();
//		return FAILED;
//	}
	tester.headSize = HEADSIZE;
	tempFile.read((char*)(&tester.iSoftwareVersion), 64);
	tester.dTimeOfBin = tester.dTimeOfBinNS * 1.0e-9;

	if(tester.iNumberOfAnalog>MAXANALOGNUM)
	{
		cout<<"The MAXANALOGNUM should be upgraded to at least "<<tester.iNumberOfAnalog<<endl;
		tempFile.close();
		return FAILED;
	}
	tempFile.close();
	
	long tempBinsNum=  (long)(1.0/tester.dFrequencyOfLaser/tester.dTimeOfBin + 0.5); //not good for yuenlong lidar
	if(tempBinsNum>MAXBINSNUM)
	{
		cout<<"The MAXBINSNUM should be upgraded to at least "<<tempBinsNum<<endl;
		return FAILED;
	}
	
	cout<<"    -looking into: "<<newFileFullName<<endl;
	
	fileSize=tempFileSize;
	fileDataHeadSize=tester.headSize;
	fileDataAnalogNum=tester.iNumberOfAnalog;
	fileDataBinsNum= 667;
	fileDataBlockSize= 64 +fileDataBinsNum*sizeof(float);
	fileDataBlockNum=fileSize/fileDataBlockSize;
#ifdef DEBUG
	cout << "laserfreq: " <<tester.dFrequencyOfLaser<<"   bintime: "<<tester.dTimeOfBin<<endl;
	cout << "fileSize: "<<fileSize<<endl;
	cout << "fileHeadSize: " << fileDataHeadSize<<endl;
	cout << "fileDataBinsNum: " << fileDataBinsNum<<endl;
	cout << "BlockSize: " << fileDataBlockSize<<endl;
	cout << "BlockNum: "<< fileDataBlockNum<<endl;
#endif

	if(fileSize%fileDataBlockSize!=0)
	{
		cout<<"The File may be incomplete"<<endl;
		return FAILED;
	}
	return SUCCEED;
}
int FileReader::FileNameAnalysis(string newFileFullName)
{
	fileFullName=newFileFullName;
	int lastSlashPos=fileFullName.rfind(SLASH);
	if(lastSlashPos!=string::npos)
	{
		filePath.assign(fileFullName,0,lastSlashPos+1);
		fileName.assign(fileFullName,lastSlashPos+1,fileFullName.size()-lastSlashPos-1);
	}
	else
	{
		filePath="";
		fileName=fileFullName;
	}
	return SUCCEED;
}

int FileReader::ReadToLine(OneLine *pLine, int indexOfBlockInFile)
{
	pLine->GetFromFile(file, fileDataHeadSize,  fileDataAnalogNum, fileDataBinsNum, indexOfBlockInFile*fileDataBlockSize);
	return SUCCEED;
}
void FileReader::ShowInfo()
{
	cout<<fileFullName<<endl;
	cout<<"file size: "<<fileSize<<endl;
	cout<<"file BlockNum: "<<fileDataBlockNum<<endl;
}

